<?php

function twoMergeSort($arr,$left,$mid,$right){
    $aIndex = $left==$mid ? -1 : $left;
    $bIndex = $mid==$right ? -1 : $mid;
    $crr = [];

    while (($aIndex >= $left && $aIndex < $mid) || ($bIndex >= $mid && $bIndex <= $right)){
        $aValue = $arr[$aIndex]??null;
        $bValue = $arr[$bIndex]??null;
        if(($aIndex < $left || $aIndex >= $mid) || ($bValue < $aValue && $bValue != null)){
            $crr[] = $bValue;
            $bIndex++;
        }else{
            $crr[] = $aValue;
            $aIndex++;
        }
    }

    for ($i = 0;$i < ($right - $left);$i++){
        $arr[$i+$left] = $crr[$i];
    }

    return $arr;
}

$arr = [1,4,7,10,15,8,11,13,19,24];

print_r(twoMergeSort($arr,0,5,9));
/**
Array
(
    [0] => 1
    [1] => 4
    [2] => 7
    [3] => 8
    [4] => 10
    [5] => 11
    [6] => 13
    [7] => 15
    [8] => 19
    [9] => 24
)
 */


